package com.lanou.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.entity.Course;
import com.lanou.entity.CourseRating;
import com.lanou.mapper.CourseMapper;
import com.lanou.mapper.CourseRatingMapper;
import com.lanou.service.CourseRatingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName CourseRatingServiceImpl
 * @Description TODO
 * @Author cuiruitong
 * @Date 2025年08月13日 15:47
 * @Version 1.0
 */
@Service
public class CourseRatingServiceImpl implements CourseRatingService {
    @Autowired
    private CourseRatingMapper courseRatingMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public int addCourseRating(CourseRating courseRating) {
        //评论增加课程里的评论数
        Course course = courseMapper.getCourseId(courseRating.getCourseId());
        course.setCommentCount(course.getCommentCount() + 1);
        courseMapper.updateCourse(course);

        return courseRatingMapper.addCourseRating(courseRating);
    }

    @Override
    public int deleteCourseRatingById(int id) {
        return courseRatingMapper.deleteCourseRatingByUserIdAndCourseId(id);
    }

    @Override
    public int updateCourseRatingByUserIdAndCourseId(CourseRating courseRating) {
        return courseRatingMapper.updateCourseRatingByUserIdAndCourseId(courseRating);
    }

    @Override
    public PageInfo<CourseRating> selectCourseRatingByCourseIdPage(int courseId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<CourseRating> courseRatings = courseRatingMapper.selectCourseRatingByCourseIdPage(courseId);
        PageInfo<CourseRating> pageInfo = new PageInfo<>(courseRatings);
        return pageInfo;
    }
}
